<?php
/**
 * Created by PhpStorm.
 * User: QM
 * Date: 2018/3/29
 * Time: 9:49
 */
namespace App\Command;

/**
 * Class MqConsumer
 * @package App\Command
 */
class MqConsumer extends AbstractBase
{
    /**
     * AgentActionReportConsumer constructor.
     */
    public function __construct()
    {
        parent::__construct("FKL");
    }
    /**
     * 消费方法
     * @param \AMQPQueue $que
     * @return mixed
     */
    public function consumerMessage(\AMQPQueue $que)
    {
        if (empty($this->msgQueue)) {
            $this->logger->info(sprintf("que【%s】等待数据", $this->queName));

            return;
        }
        foreach ($this->msgQueue as $key => $row) {
            if (empty($row['goods_id'])) {
                $this->ackData($que, $key, $row);
                $this->logger->info(sprintf("数据不齐全", $this->queName));
                continue;
            }
            try {
                $keys = "fkl.goods.".$row['goods_id'];
                if (empty($row['user_id']) || empty($row['username']) || empty($row['order_id']) || empty($row['start_time'])) {
                    $this->ackData($que, $key, $row);
                    $this->logger->info(sprintf("数据不齐全", $this->queName));
                    $this->redis->hincrby($keys, 'goods_num', +1);
                    continue;
                }
                $goodsName = $this->redis->hget($keys, "goods_name");
                $this->dbconn->insert(
                    "app_order",
                    array(
                        "goods_name" => $goodsName,
                        "username" => $row['username'],
                        "buy_num" => 1,
                        "start_time" => $row['start_time'],
                        "order_id" => $row['order_id'],
                    )
                );
            } catch (\Exception $e) {
                $this->logger->warning(sprintf(
                    "生成订单异常",
                    json_encode($row),
                    $e->getMessage()
                ));
            }
            $this->ackData($que, $key, $row, true);
        }
        $this->msgQueue = array();
    }

    /**
     * @param array $msgArray
     * @return mixed
     */
    public function proccessMsgArray($msgArray)
    {
        return $msgArray;
    }
}